/**
 * 统计报表相关的js
 */
var startDateVal;
var endDateVal;
var timeUnitVal;
layui.config({//配置并导入excel插件
    base: '../echarts/echarts.js'
}).use(['echarts', 'layer'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var excel = layui.excel;
});

$(function(){
    layui.use('laydate', function(){
        var laydate = layui.laydate,
            form  = layui.form;
        let data = new Date();
        let year = data.getFullYear();
        let month = data.getMonth() + 1;
        let day = data.getDate();
        month = month < 10?"0"+month:month;
        day = day < 10?"0"+day:day;
        var eDate =  year + '-' + month + '-' + day;
        var sDate =  year + '-' + month + '-' + day;
        var dateValue = sDate + ' - ' + eDate;
        //日期
        var time =  laydate.render({
            elem: '#globalDate2'
            ,done: function(value, date){  // choose end
                Count.loadUserRegisterCount(value, date);
            }
            ,value : sDate
            ,max: 0
        });
        Count.loadUserRegisterCount(sDate);
        Count.loadJfcsqs(null,null,"2");
        //日期范围
        var time =  laydate.render({
            elem: '#globalDate'
            ,range: "~"
            ,done: function(value, date, endDate){  // choose end
                //console.log("date callBack====>>>"+value); //得到日期生成的值，如：2017-08-18
                var startDate = value.split("~")[0];
                var endDate = value.split("~")[1];
                // var timeUnit =  $(".global-time-unit").val()
                Count.loadJfcsqs(startDate.trim(),endDate.trim(),"");
            }
            ,max: 0
        });

        layui.form.on('select(global-time-unit)', function(data){
            var dateRange = $(".global-time-unit").val();
            
            Count.loadJfcsqs(null,null,dateRange);
        });
    });
});

var  Count = {

    loadJfcsqs : function (startDate,endDate,timeUnit){
        var params = {
            startDate: startDate,
            endDate: endDate,
            timeUnit: timeUnit
        }
        CoreUtil.sendPost("/sys/home/jfcsqs", params, function (res) {
            var code = res.code;
            var data = res.data;
            if (code == 0) {
                var xAxisData = data.xAxis;
                var jfcsData = data.jfcs;

                //基于准备好的dom，初始化echarts实例
                var jfcsqs = echarts.init(document.getElementById('jfcsqs'),'shine');
                //指定图表配置项和数据
                var optionchart = {
                    title: {
                        text: '计费时长趋势图'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['计费时长']
                    },
                    xAxis: {
                        data: xAxisData
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        name: '计费时长',
                        type: 'bar', //线
                        data: jfcsData,
                        itemStyle: {
                            normal: { //线颜色
                                color: '#7cc08c'
                            }
                        },
                    }]
                };
                jfcsqs.setOption(optionchart, true);
            }

        })

    },

    //加载用户注册数据
    loadUserRegisterCount : function (value, date){
        CoreUtil.sendPost("/sys/home/tongji", {time : value}, function (res) {
            var code = res.code;
            var data = res.data;
            if (code == 0) {
                var xAxisData = data.xAxis;
                var bdcsData = data.bdcs;
                var jtsData = data.jts;
                var thscData = data.thsc;
                var jfscData = data.jfsc;
                //基于准备好的dom，初始化echarts实例
                var userRegister = echarts.init(document.getElementById('userRegisterCount'),'shine');
                //指定图表配置项和数据
                var optionchart = {
                    title: {
                        text: '数据趋势图'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['拨打次数','接通数','通话时长','计费时长']
                    },
                    xAxis: {
                        data: xAxisData
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        name: '拨打次数',
                        type: 'line', //线
                        data: bdcsData,
                        smooth: true,
                        itemStyle: {
                            normal: { //线颜色
                                color: '#96DBB9'
                            }
                        },
                    },{
                        name:'接通数',
                        type:'line',
                        data:jtsData,
                        smooth: true,
                        itemStyle:{
                            normal:{
                                color:'#72CFA1'
                            }
                        }
                    },{
                        name:'通话时长',
                        type:'line',
                        data:thscData,
                        smooth: true,
                        itemStyle:{
                            normal:{
                                color:'#11DD78'
                            }
                        }
                    },{
                        name:'计费时长',
                        type:'line',
                        data:jfscData,
                        smooth: true,
                        itemStyle:{
                            normal:{
                                color:'#00FF80'
                            }
                        }
                    }]
                };

                userRegister.setOption(optionchart, true);
            }

        })

    },
}

